/*
write by       : wuweiwei (邬畏畏)
blogs          : www.blogs.com/wsoft
WUI            : www.flybirdsoft.com/WUI

function:
渲染器
*/

var painter = function(options){
	this.contentDOM = undefined;
	this.HTML = undefined;
	if(typeof(options)=="string")
	{
		this.contentDOM = $(options)[0];
	}
	else
	{

	}
	this.HTML = this.contentDOM.innerHTML;
};

painter.prototype.add =function(options){
	if(this.options==undefined)
	{
		this.options = options;
	}
	else
	{
		for(v in options)
		{
			this.options[v] = options[v];
		}
	}	
}

painter.prototype.paint = function(options){
	/*
	options = {}
	*/
	var v;
	var reg;
	var attr;
	if(options!=undefined)
	{
		if(this.options==undefined)
		{
			this.options = options;
		}
		else
		{
			for(v in options)
			{
				this.options[v] = options[v];
			}
		}
	}
	else
	{
	}


	for(v in this.options)
	{
		reg = new RegExp("{{"+v+"}}","g");
		this.HTML = this.HTML.replace(reg,options[v]);
	}
	reg = /\{\{.{1,}\}\}/g;

	this.HTML = this.HTML.replace(reg,"");

	this.contentDOM.innerHTML = this.HTML;

	this.findChilds(this.contentDOM);

}

/*不影响事件处理*/
painter.prototype.fill = function(options){
	if(options!=undefined)
	{
		if(this.options==undefined)
		{
			this.options = options;
		}
		else
		{
			for(v in options)
			{
				this.options[v] = options[v];
			}
		}
	}
	this.findChilds(this.contentDOM);
}

/*处理数据*/
painter.prototype.findChilds = function(node){
	var children;
	var attr;
	var i;
	var options = this.options;
	var v;
	var len;

	if(!node.hasChildNodes())
	{
		return;
	}

	children = node.childNodes;
	len = children.length;

	for(i=0;i<len;i++)
	{
		if(children[i].nodeType==1)
		{
			try
			{
				attr = children[i].getAttribute("p-html");
				if(attr!=null)
				{
					if(options[attr]!=undefined)
					{
						children[i].innerHTML = options[attr];
					}
				}
			}
			catch(e)
			{;}

		}
		//if(children[i].hasChildNodes())
		{
			this.findChilds(children[i]);
		}
	}/*end for*/

}



